package ysu.sxm.ptjc.Service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import ysu.sxm.ptjc.Dao.*;
import ysu.sxm.ptjc.Service.DefectQueryService;
import ysu.sxm.ptjc.Service.DefectRecordService;
import ysu.sxm.ptjc.Service.StatisticsService;
import ysu.sxm.ptjc.pojo.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
@Async
public class StatisticsServiceImpl implements StatisticsService {
    @Autowired
    produceInfoDao dao;
    @Autowired
    DefectRecordService defectService;
    @Autowired
    DefectQueryService defectqueryService;
    @Autowired
    ProduceDao produceDao;
    @Autowired
    RecordStatisticalMapper recordStatisticalMapper;
    @Autowired
    ProRelationInfoDao proRelationInfoDao;
    @Autowired
    DefectTypeInfoDao defectTypeInfoDao;
    @Autowired
    ProduceStatisticalInfoDao produceStatisticalInfoDao;

    @Override
    public void insertDefectTypeTable(Timestamp date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<String> num = recordStatisticalMapper.getProduceNumByDate(format.format(date));
        for (int i = 0; i < num.size(); i++) {
            for (int j = 0; j <= 1; j++) {
                    List<ProduceInfo> produceInfo = recordStatisticalMapper.getProduceTypeAndLineIdByProduceNumAndLineId(num.get(i));
                    DefectTypeInfo defectTypeInfo = new DefectTypeInfo();
                    defectTypeInfo.setProduceNum(num.get(i));
                    defectTypeInfo.setProduceDate(date);
                    defectTypeInfo.setDefectDegree(j + "");
                    defectTypeInfo.setProduceType(produceInfo.get(0).getProduceType());
                    defectTypeInfo.setLineId(produceInfo.get(0).getLineId().toString());
                    CastingDefectsInfo castingDefectsInfo = recordStatisticalMapper.getCastingDefectsInfo(j + "", num.get(i), format.format(date));
                    System.out.println(castingDefectsInfo);
                    ExtrusionDefectInfo extrusionDefectInfo = recordStatisticalMapper.getExtrusionDefectInfo(j + "", num.get(i), format.format(date));
                    MachiningDefectsInfo machiningDefectsInfo = recordStatisticalMapper.getMachiningDefectsInfo(j + "", num.get(i), format.format(date));
                    PolishingDefectInfo polishingDefectInfo = recordStatisticalMapper.getPolishingDefectInfo(j + "", num.get(i), format.format(date));
                    OxidationDefectInfo oxidationDefectInfo = recordStatisticalMapper.getOxidationDefectInfo(j + "", num.get(i), format.format(date));
                    recordStatisticalMapper.insertCastingDefectsInfo(castingDefectsInfo);
                    recordStatisticalMapper.insertExtrusionDefectInfo(extrusionDefectInfo);
                    recordStatisticalMapper.insertMachiningDefectsInfo(machiningDefectsInfo);
                    recordStatisticalMapper.insertPolishingDefectInfo(polishingDefectInfo);
                    recordStatisticalMapper.insertOxidationDefectInfo(oxidationDefectInfo);
                    defectTypeInfo.setCastingDefectsId(castingDefectsInfo.getCastingDefectsId());
                    defectTypeInfo.setExtrusionDefectId(extrusionDefectInfo.getExtrusionDefectId());
                    defectTypeInfo.setMachiningDefectsId(machiningDefectsInfo.getMachiningDefectsId());
                    defectTypeInfo.setPolishingDefectId(polishingDefectInfo.getPolishingDefectId());
                    defectTypeInfo.setOxidationDefectId(oxidationDefectInfo.getOxidationDefectId());
                    if (j == 0) {
                        AssemblyDefectInfo assemblyDefectInfo = recordStatisticalMapper.getAssemblyDefectInfo(num.get(i), format.format(date));
                        recordStatisticalMapper.insertAssemblyDefectInfo(assemblyDefectInfo);
                        defectTypeInfo.setAssemblyDefectId(assemblyDefectInfo.getAssemblyDefectId());
                        defectTypeInfo.setCdpDefectId((long) 0);
                    } else {
                        defectTypeInfo.setAssemblyDefectId((long) 0);
                        CdpDefectInfo cdpDefectInfo = recordStatisticalMapper.getCdpDefectInfo(num.get(i), format.format(date));
                        recordStatisticalMapper.insertCdpDefectInfo(cdpDefectInfo);
                        defectTypeInfo.setCdpDefectId(cdpDefectInfo.getCdpDefectId());
                    }
                    defectTypeInfoDao.insert(defectTypeInfo);
                }
            }
        for (int i = 0; i < num.size(); i++) {
            List<ProduceInfo> produceInfo = recordStatisticalMapper.getProduceTypeAndLineIdByProduceNumAndLineId(num.get(i));
            ProduceStatisticalInfo produceStatisticalInfo=new ProduceStatisticalInfo();
            produceStatisticalInfo.setProduceType(produceInfo.get(0).getProduceType());
            produceStatisticalInfo.setProduceName(produceInfo.get(0).getProduceName());
            produceStatisticalInfo.setProduceNum(num.get(i));
            produceStatisticalInfo.setProduceDate(date);
            long remake=recordStatisticalMapper.getDefectCount("0",num.get(i),date.toString());
            long scrap=recordStatisticalMapper.getDefectCount("1",num.get(i),date.toString());
            long allnum=recordStatisticalMapper.getAllCountByproduceNum(num.get(i),date.toString());
            long qualifynum=allnum-scrap-remake;
            produceStatisticalInfo.setReworkNum(remake);
            produceStatisticalInfo.setScrapNum(scrap);
            produceStatisticalInfo.setQualifyNum(qualifynum);
            produceStatisticalInfo.setProduceQuantity(allnum);
            AccountInfo accountInfo=new AccountInfo();
            accountInfo.setReworkRate((double)remake/(double)allnum);
            accountInfo.setScrapRate((double)scrap/(double)allnum);
            accountInfo.setUnqualified_Rate((double)(scrap+remake)/(double)allnum);
            accountInfo.setFirstpassRate((double)(qualifynum)/(double)allnum);
            accountInfo.setTotalqualified_Rate((double)(qualifynum)/(double)allnum);
            recordStatisticalMapper.insertAccount(accountInfo);
            produceStatisticalInfo.setInfoId(accountInfo.getInfoId());
            produceStatisticalInfoDao.insert(produceStatisticalInfo);
        }
    }

    @Override
    @Scheduled(cron = "59 59 23 1/1 * ? ")
    public void dostatistic() {
        Timestamp date=new Timestamp(System.currentTimeMillis());
        insertDefectTypeTable(date);
    }
    /*
    @Scheduled(fixedDelay = 1000)
    public void task() throws InterruptedException {
        System.out.println("Thread Name : "
                + Thread.currentThread().getName()
                + "  i am a task : date ->  "
                + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        Thread.sleep(5000);
    }
*/
    /**
     * 下轮任务在本轮任务开始2秒后执行. 执行时间可忽略不计
     */
    /*
    @Scheduled(fixedRate = 2000)
    public void task2() {
        System.out.println("Thread Name : "
                + Thread.currentThread().getName()
                + "  i am a task2 : date ->  "
                + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }
    */
}
